package cn.wolfcode.p2p.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleDetailMapper;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleMapper;
import cn.wolfcode.p2p.business.query.CreditTransferQueryObject;
import cn.wolfcode.p2p.business.query.PaymentScheduleDetailQueryObject;
import cn.wolfcode.p2p.business.service.IPaymentScheduleDetailService;
import cn.wolfcode.p2p.util.Assert;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PaymentScheduleDetailServiceImpl implements IPaymentScheduleDetailService {

    @Autowired
    private PaymentScheduleDetailMapper paymentScheduleDetailMapper;

    @Override
    public void createPaymentDetail(PaymentSchedule ps, BidRequest br) {

        //获取该借款中所有的bid
        List<Bid> bids = br.getBids();
        for (int i = 0; i < bids.size(); i++) {
            //投标对象
            Bid bid = bids.get(i);
            PaymentScheduleDetail detail = new PaymentScheduleDetail();
            detail.setBidAmount(bid.getAvailableAmount());
            detail.setBidId(bid.getId());

            //第几期的信息
            detail.setMonthIndex(ps.getMonthIndex());
            detail.setDeadLine(ps.getDeadLine());
            detail.setReturnType(br.getReturnType());
            detail.setBidRequestId(br.getId());
            detail.setPaymentScheduleId(ps.getId());
            detail.setFromLoginInfo(br.getCreateUser());
            detail.setToLoginInfoId(bid.getBidUser().getId());


            //当前期每位投资人的收款对应的bid
            //收款人应收利息
            BigDecimal monthlyReceiveInterest = CalculatetUtil.calMonthlyReceiveInterest
                    (bid.getAvailableAmount(), br.getBidRequestAmount(), ps.getInterest());
            detail.setInterest(monthlyReceiveInterest);
            //应收本金 : 当前收款对应的bid的投标金额 / 借款总额 * 当前还款计划本金
            BigDecimal monthlyReceivePrincipal = CalculatetUtil.calMonthlyReceivePrincipal(bid.getAvailableAmount(), br.getBidRequestAmount(), ps.getPrincipal());
            detail.setPrincipal(monthlyReceivePrincipal);
            //应收总额
            detail.setTotalAmount(detail.getPrincipal().add(detail.getInterest()));

            paymentScheduleDetailMapper.insert(detail);

        }
    }

    @Override
    public void update(PaymentScheduleDetail detail) {
        Assert.isFalse(0 == paymentScheduleDetailMapper.updateByPrimaryKey(detail), "更新失败[乐观锁修改失败]");
    }

    @Override
    public void batchUpdateTransferStateByBidIdAndToLoginInfoId(Long bidId, Long toLoginInfoId, int transferStateNormal) {
        paymentScheduleDetailMapper.batchUpdateTransferStateByBidIdAndToLoginInfoId(bidId, toLoginInfoId, transferStateNormal);
    }

}
